package app.sevenc.lib_framework.imageloader

import android.annotation.SuppressLint
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions

/**
 * Glide图片加载框架
 *
 * @author Seven-C
 * @date 2021/8/7
 */
class GlideImpl : IImageLoader {

    @SuppressLint("CheckResult")
    override fun loadImage(
        view: ImageView,
        imageUrl: String,
        scaleType: ScaleType,
        isCircle: Boolean,
        roundingRadiusPx: Int,
        topLeft: Float,
        topRight: Float,
        bottomRight: Float,
        bottomLeft: Float
    ) {
        val rm = Glide.with(view.context).load(imageUrl)
        val transformations = ArrayList<BitmapTransformation>()
        when (scaleType) {
            ScaleType.CENTER_CROP -> transformations.add(CenterCrop())
            ScaleType.FIT_CENTER -> transformations.add(FitCenter())
        }

        when {
            isCircle -> {
                transformations.add(CircleCrop())
            }
            roundingRadiusPx > 0 -> {
                transformations.add(RoundedCorners(roundingRadiusPx))
            }
            (topLeft + topRight + bottomRight + bottomLeft) > 0 -> {
                transformations.add(GranularRoundedCorners(topLeft, topRight, bottomRight, bottomLeft))
            }
        }

        rm.apply(RequestOptions().transform(MultiTransformation(transformations)))
            .transition(DrawableTransitionOptions.withCrossFade(500))
        rm.into(view)
    }

}

